window.miniApp = (function ($window) {
    var events = {};
    var currentUser = undefined;
    var miniToken = undefined;
  // 关闭
    function close() {
        if ($window.plus) {
            if (this.getWebview().preloading) {
                this.getWebview().hide("slide-out-bottom");
            } else {
                this.getWebview().close("slide-out-bottom");
            }
        } else {
            //windows
            //window.ipcRenderer.send("");
        }
    }
    // 获取信息
    function getuserminiapp(){
      if($window.plus){

      }
    }
    // 获取用户信息
    function getUser() {
        // if ($window.plus) {
        //     return this.getWebview().user;
        // } else {
        //     //windows
        // }
        return currentUser;
    }
    function setUser(jsonStr) {
        let user = JSON.parse(jsonStr);
        currentUser = user;
    }
    // 获取token
    function getMiniToken() {
        // if ($window.plus) {
        //     return this.getWebview().miniToken;
        // } else {
        //     //windows
        // }
        return miniToken;
    }

    function setMiniToken(token) {
        miniToken = token;
    }
    //
    function getWebview() {
        if ($window.plus) {
            return $window.plus.webview.currentWebview();
        } else {
            //windows
        }
    }
    getWebview()
    function login(callback) {
        var user = this.getUser();
        if (user && user.userId && user.loginId) {
            callback({
                success: true,
                user: user
            });
        } else if (window.plus) {
            plus.webview.getLaunchWebview().evalJS("vue.$f7.views.current.router.navigate(vue.$framework.config.pages.login + '?miniApp=" + this.getWebview().appCode + "')");
            this.close();
        } else {
            //windows
        }
    }

    function addEventListener(key, func) {
        events[key] = func;
    }

    function execute(key, callback) {
        var isSuccess = false, value = undefined;
        if (events[key] && typeof (events[key]) == "function") {
            value = events[key]();
            isSuccess = true;
        }
        if (callback) {
            callback(isSuccess, value);
        }
    }

    function hookAjax(funs) {
        window._ahrealxhr = window._ahrealxhr || XMLHttpRequest
        XMLHttpRequest = function () {
            this.xhr = new window._ahrealxhr;
            for (var attr in this.xhr) {
                var type = "";
                try {
                    type = typeof this.xhr[attr]
                } catch (e) { }
                if (type === "function") {
                    this[attr] = hookfun(attr);
                } else {
                    Object.defineProperty(this, attr, {
                        get: getFactory(attr),
                        set: setFactory(attr)
                    })
                }
            }
        }

        function getFactory(attr) {
            return function () {
                return this.hasOwnProperty(attr + "_") ? this[attr + "_"] : this.xhr[attr];
            }
        }

        function setFactory(attr) {
            return function (f) {
                var xhr = this.xhr;
                var that = this;
                if (attr.indexOf("on") != 0) {
                    this[attr + "_"] = f;
                    return;
                }
                if (funs[attr]) {
                    xhr[attr] = function () {
                        funs[attr](that) || f.apply(xhr, arguments);
                    }
                } else {
                    xhr[attr] = f;
                }
            }
        }

        function hookfun(fun) {
            return function () {
                var args = [].slice.call(arguments)
                if (funs[fun] && funs[fun].call(this, args, this.xhr)) {
                    return;
                }
                return this.xhr[fun].apply(this.xhr, args);
            }
        }
        return window._ahrealxhr;
    }

    function unHookAjax() {
        if (window._ahrealxhr) XMLHttpRequest = window._ahrealxhr;
        window._ahrealxhr = undefined;
    }

    function init() {
        hookAjax({
            //hook callbacks
            onreadystatechange: function (xhr) {
            },
            onload: function (xhr) {
            },
            open: function (arg, xhr) {
            },
            send: function (arg, xhr) {
                xhr.setRequestHeader("mini-token", miniApp.getMiniToken());
            },
            setRequestHeader: function (arg, xhr) {
            }
        })
    }
    init();
    return {
        close: close,
        getUser: getUser,
        setUser: setUser,
        addEventListener: addEventListener,
        execute: execute,
        events: events,
        getMiniToken: getMiniToken,
        setMiniToken: setMiniToken,
        getWebview: getWebview,
        login: login,
    }
})(window);
